package com.qiwx.pigeon.fragment;

import android.os.Bundle;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.adapter.query.TabPagerAdapter;
import com.qiwx.pigeon.base.BaseFragment;
import com.qiwx.pigeon.base.BaseMVPFragment;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.fragment.produce.CheckEggOperationFragment;
import com.qiwx.pigeon.fragment.produce.DailyOperationFragment;
import com.qiwx.pigeon.fragment.produce.DataChartFragment;
import com.qiwx.pigeon.fragment.produce.GetOutOperationFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-23 14:00
 * 生产
 **/
public class ProduceFragment extends BaseFragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.lin_fragment_tab_viewpager)
    LinearLayout linFragmentTabViewpager;
    private List<Fragment> fragmentList = new ArrayList<>();
    TabPagerAdapter adapter;
    String[] titles;

    int produceStatus = CageStatusConfig.PRODUCE_CD;
    Bundle bundle;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_produce;
    }

    @Override
    public void initAfterOnCreate() {
//        titles = new String[]{"日常工段", "查蛋工段", "空窝操作", "出栏操作"};
//        titles = new String[]{"日常工段", "查蛋工段", "出栏操作"};
        titles = new String[]{"日常工段", "查蛋工段"};
        bundle = getArguments();
        if (bundle != null) {
            boolean isGoCd = bundle.getBoolean("isGoCD");
            if (isGoCd) {
                produceStatus = bundle.getInt("produce_status");
            }
        }
        initFragments();
        initView();
    }

    public void showCheckEggFragment(int produce_status) {

        if (viewPager != null) {
            CheckEggOperationFragment cf = (CheckEggOperationFragment) fragmentList.get(1);
            if (produce_status == CageStatusConfig.PRODUCE_TW) {
                cf.setAdjust();
            } else if (produce_status == CageStatusConfig.PRODUCE_CD) {
                cf.setCd();
            } else if (produce_status == CageStatusConfig.PRODUCE_CZ) {
                cf.setCz();
            }
            viewPager.setCurrentItem(1);
        } else {
            produceStatus = produce_status;
        }


    }


    private void initView() {
        adapter = new TabPagerAdapter(getChildFragmentManager(), titles, fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(4);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中某个tab
                tab.getPosition();

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //当tab从选择到未选择
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //已经选中tab后的重复点击tab
            }
        });
        if (getActivity() instanceof MainActivity) {
            ((MainActivity) getActivity()).setWareHouseListener(new MainActivity.WareHouseListener() {
                @Override
                public void onWhSuccess(List<WareHouseBean> list) {

                }

                @Override
                public void onSelectWareHouse(WareHouseBean wareHouseBean) {
                    for (int i = 0; i < fragmentList.size(); i++) {
                        ((BaseMVPFragment) fragmentList.get(i)).onReFreshData();
                    }

                }
            });
        }
        if (produceStatus == CageStatusConfig.PRODUCE_TW
                || produceStatus == CageStatusConfig.PRODUCE_CD
                || produceStatus == CageStatusConfig.PRODUCE_CZ
        ) {
            viewPager.setCurrentItem(1);
        }

    }


    @Override
    public void dbUpdate() {
//        initSpinner();
    }

    private void initFragments() {
        fragmentList.clear();
        fragmentList.add(new DailyOperationFragment());
        CheckEggOperationFragment cf = new CheckEggOperationFragment();
        if (bundle != null) {
            cf.setArguments(bundle);
        }
        fragmentList.add(cf);
//        fragmentList.add(new CheckKWOperationFragment());
//        fragmentList.add(new DataChartFragment());
//        fragmentList.add(new GetOutOperationFragment());
    }
}
